package com.xr.sy.modules.message.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xr.common.constant.Constant;
import com.xr.common.exception.ErrorCode;
import com.xr.common.exception.SyException;
import com.xr.common.page.PageData;
import com.xr.common.service.impl.BaseServiceImpl;
import com.xr.common.utils.ConvertUtils;
import com.xr.sy.modules.message.dao.SysMailTemplateDao;
import com.xr.sy.modules.message.dto.SysMailTemplateDTO;
import com.xr.sy.modules.message.email.EmailUtils;
import com.xr.sy.modules.message.entity.SysMailTemplateEntity;
import com.xr.sy.modules.message.service.SysMailTemplateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
public class SysMailTemplateServiceImpl extends BaseServiceImpl<SysMailTemplateDao, SysMailTemplateEntity> implements SysMailTemplateService {
    @Lazy
    @Autowired
    private EmailUtils emailUtils;

    @Override
    public PageData<SysMailTemplateDTO> page(Map<String, Object> params) {
        IPage<SysMailTemplateEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );
        return getPageData(page, SysMailTemplateDTO.class);
    }

    private QueryWrapper<SysMailTemplateEntity> getWrapper(Map<String, Object> params) {
        String name = (String) params.get("name");

        QueryWrapper<SysMailTemplateEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name), "name", name);

        return wrapper;
    }

    @Override
    public SysMailTemplateDTO get(Long id) {
        SysMailTemplateEntity entity = baseDao.selectById(id);
        return ConvertUtils.sourceToTarget(entity, SysMailTemplateDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysMailTemplateDTO dto) {
        SysMailTemplateEntity entity = ConvertUtils.sourceToTarget(dto, SysMailTemplateEntity.class);
        insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysMailTemplateDTO dto) {
        SysMailTemplateEntity entity = ConvertUtils.sourceToTarget(dto, SysMailTemplateEntity.class);
        updateById(entity);
    }

    @Override
    public boolean sendMail(Long id, String mailTo, String mailCc, String params) throws Exception {
        Map<String, Object> map = null;
        try {
            if (StringUtils.isNotEmpty(params)) {
                map = JSON.parseObject(params, Map.class);
            }
        } catch (Exception e) {
            throw new SyException(ErrorCode.JSON_FORMAT_ERROR);
        }
        String[] to = new String[]{mailTo};
        String[] cc = StringUtils.isBlank(mailCc) ? null : new String[]{mailCc};

        return emailUtils.sendMail(id, to, cc, map);
    }


}
